package com.one.common.common.order.binder;

import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.one.common.R;
import com.one.common.callback.OnBinderItemClickListener;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.utils.ScreenUtils;
import com.one.common.utils.StringUtils;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;

/**
 * creator: zhulunjun
 * time:    2018/8/9
 * describe: 添加
 */
public class AddMaterialBinder extends BaseItemBinder<AddMaterialItem> {
    private OnBinderItemClickListener<AddMaterialItem> onBinderItemClickListener;

    public AddMaterialBinder(OnBinderItemClickListener<AddMaterialItem> onBinderItemClickListener) {
        super(R.layout.item_material_add);
        this.onBinderItemClickListener = onBinderItemClickListener;
    }

    @Override
    protected void bindView(@NonNull final BaseViewHolderMulti holder, @NonNull final AddMaterialItem item) {
        holder.getView(R.id.tv_select_material).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBinderItemClickListener.onItemClick(v, holder.getLayoutPosition(), item);
            }
        });
        holder.setText(R.id.tv_select_material, item.getName());

        final EditText etNum = holder.getView(R.id.et_num);
        final EditText etPrice = holder.getView(R.id.et_price);
        View vLine = holder.getView(R.id.v_line);
        if (item.isFree()) {
            vLine.setVisibility(View.GONE);
            etPrice.setVisibility(View.GONE);
        } else {
            etPrice.setVisibility(View.VISIBLE);
            vLine.setVisibility(View.VISIBLE);
            if (item.getPrice() > 0) {
                etPrice.setText(item.getPrice() + "");
            }
        }
        if (item.getNum() > 0) {
            etNum.setText(item.getNum() + "");
        }

        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtils.isBlank(s.toString())) {
                    return;
                }
                item.setNum(s.toString());
//                int num = Integer.parseInt(s.toString());
//                if (num > 0) {
//                    item.setNum(s.toString());
//                } else {
//                    etNum.setText("");
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
        etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                item.setPrice(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


}
